
package ilves;

import java.io.Serializable;
import java.util.Scanner;
import java.util.TreeSet;

/**
 *
 * @author AQ
 */
public class UserInterface implements Serializable {
    private Scanner scanner = new Scanner(System.in);
    private Course[] courseList;
    private TreeSet<Course> myCourses;

    public UserInterface() {
        this.myCourses = new TreeSet<Course>();
        try {
            courseList = (new UrlReader()).luodutKurssit();
        }
        catch(Exception e) { }
    }

    /**
     * Prints the main menu of the program.
     */
    public void printMainMenu() {
        int input;
        if (scanner == null) scanner = new Scanner(System.in);
        do {
            System.out.println("\nValikko ---------------------");
            System.out.println("1. Näytä kurssiluettelo");
            System.out.println("2. Näytä valitut kurssisi");
            System.out.println("3. Näytä viikkoaikataulusi");
            System.out.println("4. Lisää kurssi");
            System.out.println("5. Näytä suoritetut kurssisi");
            System.out.println("0. Lopeta ohjelma");
            input = inputChoice();
            switch(input) {
                case 1: printCourseList(); break;
                case 2: printOwnCourses(); break;
                case 3: printSchedule(); break;
                case 4: addCourseMenu(); break;
                case 5: printFinishedCourses(); break;
                case 0: {
                    scanner = null;
                    break;
                }
                default: System.out.println("Virheellinen komento!");
            }
        }
        while(input != 0);
    }

    /**
     * Prints the list of available courses.
     */
    public void printCourseList() {
        int i = 0;
        while(courseList[i] != null) {
            System.out.println((i+1)+"\t"+courseList[i]);
            i++;
        }
        
    }

    /**
     * Lists the courses which the user has chosen from the course list.
     */
    public void printOwnCourses() {
        System.out.println("\nValitut kurssit ------------------------");
        int i = 1;
        for (Course x : this.myCourses)
            System.out.println(i++ +". "+ x);
        System.out.println("\nValikko -------------------------------");
        System.out.println("1. Lisää opetustapahtuma");
        System.out.println("2. Poista kurssi valituista kursseista");
        System.out.println("3. Merkitse kurssi suoritetuksi");
        System.out.println("0. Palaa päävalikkoon");

        int input = inputChoice();

        switch(input) {
            case 1: break; // TODO: Course.courseList.add tms.
            case 2: {
                System.out.println("Valitse poistettava kurssi.");
                int kurssi = inputChoice();
                this.myCourses.remove(this.getCourse(kurssi));
                break;
            } // TODO: Course.courseList.delete tms.
            case 3: {
                        System.out.println("Valitse suoritettu kurssi.");
                        int kurssi = inputChoice();
                        this.getCourse(kurssi).suorita();
                        break;
                    }// TODO: Course.moveToFinished tms.
            case 0: return;
            default: System.out.println("Virheellinen komento!");
        }
    }

    public void printSchedule() {
        // TODO: Joku aikataululuokka tms.?
    }

    public void addCourseMenu() {
        printCourseList();
        System.out.print("Valitse kurssi: ");
        int course_number = scanner.nextInt(); scanner.nextLine();
        this.myCourses.add(this.courseList[course_number-1]);
    }

    public void printFinishedCourses() {
        System.out.println("\nSuoritetut kurssit---------------------");
        int i = 1;
        for (Course x : this.myCourses)
            if(x.getSuoritettu() == true)
                System.out.println(i++ +". "+ x);
    }

    /**
     * Asks the user for a number to input and returns it.
     */
    private int inputChoice() {
        System.out.print("Syötä valintasi: ");
        int input = scanner.nextInt(); scanner.nextLine();

        return input;
    }

    /**
     * Iterates the myCourses TreeSet and returns the Course pointed by index i.
     * @param i The index of the course on a printed list (alphabetical, ascending)
     * @return The Course-object pointed by i, null if i is negative or bigger than the number of courses.
     */
    private Course getCourse(int i) {
      if (i < 1) return null;
      for (Course x : this.myCourses) {
        i = i - 1;
        if (i == 0) {
            return x;
        }
      }
      return null;
    }

    public static void run() {
        
    }
}
